package com.cskaoyan.order.service.impl;

import com.cskaoyan.mall.commons.exception.ExceptionProcessorUtils;
import com.cskaoyan.mall.order.constant.OrderRetCode;
import com.cskaoyan.order.converter.OrderConverter;
import com.cskaoyan.order.dal.entitys.Order;
import com.cskaoyan.order.dal.entitys.OrderItem;
import com.cskaoyan.order.dal.entitys.OrderShipping;
import com.cskaoyan.order.dal.persistence.OrderItemMapper;
import com.cskaoyan.order.dal.persistence.OrderMapper;
import com.cskaoyan.order.dal.persistence.OrderShippingMapper;
import com.cskaoyan.order.dto.*;
import com.cskaoyan.order.form.OrderDetail;
import com.cskaoyan.order.form.PageResponse;
import com.cskaoyan.order.service.OrderQueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ExceptionHandler;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 苏东宇
 * @since 2022/07/29 15:51
 */
@Service
public class OrderQueryServiceImpl implements OrderQueryService {
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    OrderItemMapper orderItemMapper;
    @Autowired
    OrderShippingMapper orderShippingMapper;

    @Autowired
    OrderConverter orderConverter;

    @Override
    public OrderListResponse orderList(OrderListRequest request) {
        OrderListResponse orderListResponse = new OrderListResponse();
        // PageResponse pageResponse = new PageResponse();



        try {
            request.requestCheck();
            Example example = new Example(Order.class);
            example.createCriteria()
                    .andEqualTo("userId",request.getUserId());

            List<Order> orders = orderMapper.selectByExample(example);

            List<OrderDetailInfo> orderDetailInfos = orderConverter.orders2detail(orders);
            ArrayList<OrderDetailInfo> orderDetailInfosByPage = new ArrayList<>();

            int index = (request.getPage() - 1) * request.getSize();
            int size = (request.getPage()) * request.getSize();
            if (size > orders.size()){
                size = orders.size();
            }

            for (int i = index; i < size; i++) {


                Example example1 = new Example(OrderItem.class);
                example1.createCriteria()
                        .andEqualTo("orderId",orderDetailInfos.get(i).getOrderId());
                List<OrderItem> orderItems = orderItemMapper.selectByExample(example1);
                List<OrderItemDto> orderItemDtos = orderConverter.item2dto(orderItems);
                orderDetailInfos.get(i).setOrderItemDto(orderItemDtos);
                orderDetailInfosByPage.add(orderDetailInfos.get(i));
            }
            // for (OrderDetailInfo orderDetailInfo : orderDetailInfos) {
            //
            // }


            orderListResponse.setData(orderDetailInfosByPage);
            orderListResponse.setCode(OrderRetCode.SUCCESS.getCode());
            orderListResponse.setMsg(OrderRetCode.SUCCESS.getMessage());
            orderListResponse.setTotal(new Long(orders.size()));
        }catch (Exception e){
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(orderListResponse,e);
        }


        return orderListResponse;
    }

    @Override
    public OrderDetail orderDetail(OrderDetailRequest request) {

        OrderDetail orderDetail = new OrderDetail();

        try {
            request.requestCheck();
            //查询订单信息
            Order order = orderMapper.selectByPrimaryKey(request.getOrderId());


            //查询商品条目信息
            Example example = new Example(OrderItem.class);
            example.createCriteria()
                    .andEqualTo("orderId",request.getOrderId());
            List<OrderItem> orderItems = orderItemMapper.selectByExample(example);
            List<OrderItemDto> orderItemDtos = orderConverter.item2dto(orderItems);

            //查询物流信息
            Example shippingExample = new Example(OrderShipping.class);
            shippingExample.createCriteria()
                    .andEqualTo("orderId",request.getOrderId());
            List<OrderShipping> orderShippings = orderShippingMapper.selectByExample(shippingExample);
            if (orderShippings != null){
                OrderShipping orderShipping = orderShippings.get(0);
                OrderShippingDto orderShippingDto = orderConverter.shipping2dto(orderShipping);
                orderDetail.setTel(orderShippingDto.getReceiverPhone());
                orderDetail.setStreetName(orderShippingDto.getReceiverAddress());


            }



            //将查询到的商品条目信息和物流信息赋给response

            orderDetail.setOrderTotal(order.getPayment());
            orderDetail.setUserId(order.getUserId());
            orderDetail.setUserName(order.getBuyerNick());
            orderDetail.setGoodsList(orderItemDtos);
            orderDetail.setOrderStatus(order.getStatus());

            orderDetail.setCode(OrderRetCode.SUCCESS.getCode());
            orderDetail.setMsg(OrderRetCode.SUCCESS.getMessage());



        }catch (Exception e){
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(orderDetail,e);
        }



        return orderDetail;
    }
}
